package com.bds.btcdc.web.action;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bds.btcdc.api.model.OrderInfo;
import com.bds.btcdc.web.model.record.OrderRecord;
import com.bds.btcdc.api.model.Order;
import com.bds.btcdc.web.service.ITradeService;
import com.bds.btcdc.web.utils.Const;
import com.bds.btcdc.web.utils.CookieUtils;
import com.bds.btcdc.web.utils.OrderConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bds.btcdc.api.model.Result;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: kevin
 * @date: 2018/5/2
 * @description: 现货交易Controller
 */
@Controller
@RequestMapping("/spot")
public class SpotTradeAction {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ITradeService iTradeService;

	@RequestMapping("/trade")
	public String spotTrade(Map<String, Object> map) {
		map.put("wsurl", Const.WS_URL);
		return "ftl/spot/spot_trade";
	}


	@RequestMapping("/record")
	public String tradeRecord(HttpServletRequest request,Map<String, Object> map) {
        Long user_id=CookieUtils.getUserIdfromCookie(request);
        List<OrderRecord> record_list = iTradeService.getTradeRecord(user_id);
        map.put("list",record_list);
        return "ftl/spot/trade_record";
	}

	@RequestMapping("/record/refresh")
	@ResponseBody
	public Result recordRefresh(HttpServletRequest request, HttpServletResponse response,Map<String, Object> map) {
		Long user_id=CookieUtils.getUserIdfromCookie(request);
		Result rst=iTradeService.refreshRecordStatus(user_id);
		return rst;
	}


	@RequestMapping("/order")
	@ResponseBody
	public Result order(HttpServletRequest request, String exchange, String symbol, double num, double price,String type) {
		Result rst = new Result();
		Long user_id=CookieUtils.getUserIdfromCookie(request);
		if(user_id==null){
		    rst.setResult(Const.FAIL,"无登陆信息");
		    return rst;
        }
        Order order=new Order();
        order.setExchange(exchange);
        order.setUserId(user_id);
        order.setNum(num);
        order.setPrice(price);
        order.setSymbol(symbol);
        order.setType(type);
		Result order_rst = iTradeService.makeOrder(order);
		return order_rst;
	}

}
